package org.example.serivce.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.example.bean.GoodsBean;
import org.example.mapper.IGoodsMapper;
import org.example.serivce.IGoodsService;

import java.time.LocalDate;
import java.util.List;

public class GoodsServiceImpl extends BaseService implements IGoodsService {
    @Override
    public void add(GoodsBean goods) {

        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.add(goods);
        session.commit();
        session.close();
    }

    @Override
    public void del(Integer id) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.del(id);
        session.commit();
        session.close();
    }

    @Override
    public void update(Integer id, Integer newPrice) {

        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.update(id,newPrice);
        session.commit();
        session.close();
    }

    @Override
    public PageInfo<GoodsBean> selectByNamePrice(Integer pageNum, String name, Integer minPrice, Integer maxPrice) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        PageHelper.startPage(pageNum,3);
        PageInfo<GoodsBean> pg = PageInfo.of(mapper.selectByNamePrice(name,minPrice,maxPrice));
        session.close();
        return pg;
    }

    @Override
    public PageInfo<GoodsBean> selectByFactoryDate(Integer pageNum,String factory, LocalDate starDate, LocalDate endDate) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        PageHelper.startPage(pageNum,5);
        PageInfo<GoodsBean> pg = PageInfo.of(mapper.selectByFactoryDate(factory,starDate,endDate));
        session.close();
        return pg;
    }

    @Override
    public void addMore(List<GoodsBean> list) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.addMore(list);
        session.commit();
        session.close();
    }

    @Override
    public void delMore(int[] idArray) {
        SqlSession session = this.getSession();
        IGoodsMapper mapper = session.getMapper(IGoodsMapper.class);
        mapper.delMore(idArray);
        session.commit();
        session.close();
    }
}
